home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / fghedge.zip / HEDGE.C < prev    next >
Text File  |  1993-10-11  |  23KB  |  876 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  Hedge.c                                                                   *
  4. *                                                                            *
  5. *  Copyright 1993 Diana Gruber. All rights reserved.                         *
  6. *                                                                            *
  7. *  This program is copyrighted freeware, distributed with source code.       *
  8. *  Code is provided "as is" without any warranties. You may use portions     *
  9. *  of this code in your own programs as long as you do not use more than     *
  10. *  50% of the code in any one program.                                       *
  11. *                                                                            *
  12. *  This code is provided for instructional purposes. It requires Fastgraph   *
  13. *  or Fastgraph/Light to link.                                               *
  14. *                                                                            *
  15. *  For more information, including the compilation and linking commands for  *
  16. *  all supported compilers, please see the HEDGE.DOC file.                   *
  17. *                                                                            *
  18. \****************************************************************************/
  19.  
  20. #include "defs.h"
  21. #define NMAZES 7
  22.  
  23. /****************************************************************************\
  24. *                                                                            *
  25. *  main -- all functions are in alphabetical order except this one           *
  26. *                                                                            *
  27. \****************************************************************************/
  28.  
  29. void main()
  30. {
  31.    register int i,j;
  32.    int n;
  33.  
  34.    char *maze_name[] =
  35.    {
  36.       "maze001.lev",
  37.       "maze002.lev",
  38.       "maze003.lev",
  39.       "maze004.lev",
  40.       "maze005.lev",
  41.       "maze006.lev",
  42.       "maze007.lev"
  43.    };
  44.  
  45.    /* open the attribute file and read the attributes */
  46.  
  47.    if ((tstream = fopen("hedge.att","rb")) == NULL)
  48.    {
  49.       sprintf(abort_string,"HEDGE.ATT not found.");
  50.       abort_game();
  51.    }
  52.    fread(attributes,sizeof(char),240,tstream);
  53.    fclose(tstream);
  54.  
  55.    /* initialize SVGA graphics */
  56.  
  57.    init_graphics();
  58.    fg_tcmask(1);
  59.  
  60.    intro_screen();
  61.  
  62.    /* load the dub diner file on the hidden page */
  63.  
  64.    fg_setpage(HIDDEN);
  65.    fg_erase();
  66.    fg_move(80,60);
  67.    fg_showpcx("DUBDINER.PCX",2);
  68.  
  69.    /* copy it to the logical page (EMS or XMS) */
  70.  
  71.    fg_copypage(HIDDEN,SPARE);
  72.  
  73.    /* display the maze file on the hidden page */
  74.  
  75.    fg_move(0,0);
  76.    fg_showpcx("HEDGE.PCX",1);
  77.  
  78.    fg_setpage(VISUAL);
  79.    fg_setcolor(0);
  80.    center_string("Please wait...",400,300);
  81.    fg_setcolor(7);
  82.    center_string("Press any key to continue.",400,300);
  83.    fg_waitkey();
  84.  
  85.    for (n = 0; n < NMAZES; n++)
  86.    {
  87.       fg_setrgb(7,63,63,63);
  88.       fg_setrgb(5,63,63,63);
  89.       fg_setrgb(2,63,63,63);
  90.       fg_mousevis(OFF);
  91.       fg_copypage(SPARE,VISUAL);
  92.  
  93.       load_maze(maze_name[n]);
  94.  
  95.       for (i = 0; i <50; i++)
  96.       {
  97.          for (j = 0; j <37; j++)
  98.             visited[i][j] = 0;
  99.       }
  100.  
  101.       for (i = 0; i < 2000; i++)
  102.          path[i] = -1;
  103.       path_index = 1;
  104.  
  105.       flushkey();
  106.       if (n == 0) help_window();
  107.  
  108.       fg_mousevis(ON);
  109.       run_maze();
  110.    }
  111.    quit_graphics();
  112. }
  113.  
  114. /****************************************************************************\
  115. *                                                                            *
  116. *  calc_truepath -- recursive algorithm to solve maze                        *
  117. *                                                                            *
  118. \****************************************************************************/
  119.  
  120. int calc_truepath(int i,int j)
  121. {
  122.    int tile,nexttile;
  123.    int attr,nextattr;
  124.    int x1,x2,y1,y2;
  125.    int found;
  126.    int index;
  127.    int stall_time;
  128.    int left,right,top,bottom;
  129.    int horizontal,vertical;
  130.    int nextcross;
  131.  
  132.    stall_time = clockspeed/4;
  133.    visited[i][j] = TRUE;
  134.    index = path_index;
  135.  
  136.    while(1)
  137.    {
  138.       fg_stall(stall_time);
  139.  
  140.       tile = layout[i][j];
  141.       attr = attributes[tile];
  142.       found = FALSE;
  143.  
  144.       x1 = i*16 + 6;
  145.       x2 = x1 + 4;
  146.       y1 = j*16 + 6;
  147.       y2 = y1 + 4;
  148.  
  149.       left       = attr&LEFT;
  150.       right      = attr&RIGHT;
  151.       top        = attr&TOP;
  152.       bottom     = attr&BOTTOM;
  153.       vertical   = attr&VERTICAL;
  154.       horizontal = attr&HORIZONTAL;
  155.  
  156.       if (horizontal || vertical)
  157.       {
  158.          if (path[index-1] == LEFT || path[index-1] == RIGHT)
  159.          {
  160.             top = FALSE; bottom = FALSE;
  161.          }
  162.          else if (path[index-1] == TOP || path[index-1] == BOTTOM)
  163.          {
  164.             left = FALSE; right = FALSE;
  165.          }
  166.       }
  167.  
  168.       /* check right */
  169.  
  170.       if (j < MAXCOLS-1 && !found && !(path[index-1] == LEFT))
  171.       {
  172.          nexttile = layout[i+1][j];
  173.          nextattr = attributes[nexttile];
  174.          nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
  175.          if (!visited[i+1][j] || (nextcross && (visited[i+1][j] != HORIZONTAL)))
  176.          {
  177.             if (right && nextattr&LEFT)
  178.             {
  179.                found = TRUE;
  180.                path[index++] = RIGHT;
  181.                i++;
  182.                visited[i][j] = HORIZONTAL;
  183.                x2 = i*16 + 10;
  184.                if (vertical)
  185.                  x1 += 9;
  186.                if (nextattr&VERTICAL)
  187.                  x2 -= 9;
  188.             }
  189.          }
  190.       }
  191.  
  192.       /* check UP */
  193.  
  194.       if (j > 0 && !found && !(path[index-1] == BOTTOM))
  195.       {
  196.          nexttile = layout[i][j-1];
  197.          nextattr = attributes[nexttile];
  198.          nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
  199.          if (!visited[i][j-1] || (nextcross && (visited[i][j-1] != VERTICAL)))
  200.          {
  201.             if (top && nextattr&BOTTOM)
  202.             {
  203.                found = TRUE;
  204.                path[index++] = TOP;
  205.                j--;
  206.                visited[i][j] = VERTICAL;
  207.                y1 = j*16 + 6;
  208.                if (nextattr&HORIZONTAL)
  209.                  y1 += 9;
  210.                if (horizontal)
  211.                  y2 -= 9;
  212.             }
  213.          }
  214.       }
  215.  
  216.       /* check down */
  217.  
  218.       if (j < MAXROWS-1 && !found && !(path[index-1] == TOP))
  219.       {
  220.          nexttile = layout[i][j+1];
  221.          nextattr = attributes[nexttile];
  222.          nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
  223.          if (!visited[i][j+1] || (nextcross && (visited[i][j+1] != VERTICAL)))
  224.          {
  225.             if (bottom && nextattr&TOP)
  226.             {
  227.                found = TRUE;
  228.                path[index++] = BOTTOM;
  229.                j++;
  230.                visited[i][j] = VERTICAL;
  231.                y2 = j*16 + 10;
  232.                if (horizontal)
  233.                  y1 += 9;
  234.                if (nextattr&HORIZONTAL)
  235.                  y2 -= 9;
  236.             }
  237.          }
  238.       }
  239.  
  240.       /* check left */
  241.  
  242.       if (i > 0 && !found && !(path[index-1] == RIGHT))
  243.       {
  244.          nexttile = layout[i-1][j];
  245.          nextattr = attributes[nexttile];
  246.          nextcross = ((nextattr&VERTICAL) + (nextattr&HORIZONTAL));
  247.          if (!visited[i-1][j] || (nextcross && (visited[i-1][j] != HORIZONTAL)))
  248.          {
  249.             if (left && nextattr&RIGHT)
  250.             {
  251.                found = TRUE;
  252.                path[index++] = LEFT;
  253.                i--;
  254.                visited[i][j] = HORIZONTAL;
  255.                x1 = i*16 + 6;
  256.                if (nextattr&VERTICAL)
  257.                  x1 += 9;
  258.                if (vertical)
  259.                  x2 -= 9;
  260.             }
  261.          }
  262.       }
  263.  
  264.       /* adjacent tile found */
  265.  
  266.       if (found)
  267.       {
  268.          fg_setcolor(0);
  269.          fg_rect(x1,x2,y1,y2);
  270.          if (is_a_vertex(nexttile))
  271.          {
  272.             path_index = index;
  273.  
  274.             /* You are at a vertex, do a recursive function call */
  275.  
  276.